﻿using System;
using System.Linq;
using System.Web.Mvc;
using CP_SchoolForChildren_v0._1.Controllers.Support;
using CP_SchoolForChildren_v0._1.Models.DataModel;
using CP_SchoolForChildren_v0._1.Models.RelatedModel;

namespace CP_SchoolForChildren_v0._1.Controllers.BE
{
    public class ReferenceStandardController : Controller
    {
        //
        // GET: /ReferenceStandard/
        FunctionController fc = new FunctionController();
        SFCMessage sfcMessage = new SFCMessage();


        [Authorize(Roles = "Admin")]
        public ActionResult Index()
        {
            return View();
        }


        public ActionResult RankSchool()
        {
            fc.RankAllSchool();
            return View();
        }

        #region Reference Standard

        [HttpPost]
        public JsonResult ListReferenceStandard()
        {
            try
            {
                
                var record = fc.db.ReferenceStandards.ToList();
                return Json(new { Result = "OK", Records = record });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.ToString() });
            }
        }

        //[HttpPost]
        //public JsonResult CreateReferenceStandard(ReferenceStandard referencestandard)
        //{
        //    try
        //    {
        //        if (!ModelState.IsValid)
        //        {
        //            return Json(new
        //            {
        //                Result = "ERROR",
        //                Message = "Form is not valid! " +
        //                "Please correct it and try again."
        //            });
        //        }                
        //        var record = fc.db.ReferenceStandards.Add(referencestandard);
        //        fc.db.SaveChanges();
        //        return Json(new { Result = "OK", Record = record });
        //    }
        //    catch (Exception ex)
        //    {
        //        return Json(new { Result = "ERROR", Message = ex.ToString() });
        //    }
        //}

        [HttpPost]
        public JsonResult UpdateReferenceStandard(ReferenceStandard referencestandard)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "ERROR", Message = "Form is not valid! Please correct it and try again." });
                }
                var updaters = fc.db.ReferenceStandards.
                    First(rs => rs.ReferenceStandardID == referencestandard.ReferenceStandardID);

                updaters.Description = referencestandard.Description;
                updaters.Standard = referencestandard.Standard;
                
                var record = fc.db.SaveChanges();
                return Json(new { Result = "OK", Record = record });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.ToString() });
            }
        }

        //[HttpPost]
        //public JsonResult DeleteReferenceStandard(ReferenceStandard referencestandard)
        //{
        //    try
        //    {
        //        if (!ModelState.IsValid)
        //        {
        //            return Json(new { Result = "ERROR", Message = "Form is not valid! Please correct it and try again." });
        //        }
        //        fc.db.Entry(referencestandard).State = System.Data.EntityState.Deleted;
        //        var record = fc.db.SaveChanges();
        //        return Json(new { Result = "OK", Record = record });
        //    }
        //    catch (Exception ex)
        //    {
        //        return Json(new { Result = "ERROR", Message = ex.ToString() });
        //    }
        //}
        #endregion

        
    }
}
